<?php
class channelController extends adminController{
    protected $layout = 'layout';
    //分类首页
    public function index(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result =  model('channel')->delClass($fid);
            if($result){
                echo 1;
            }else{
                echo '删除失败!请确认是否包含子分类货对应模型中有数据';
            } 
        }else{
            $this->pid = intval($_GET['id']);  //当前ID
            $condition['id'] = $this->pid;
            $listRows = 20;
            $url = url('channel/index',array('page'=>'{page}'));
            $allcount = model('channel')->count('pid='.$this->pid);
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->info = model('channel')->find($condition,'type');
            $this->list = model('channel')->classList($this->pid,$limit);
            $this->path = model('channel')->flpath($this->pid,'channel/index');
            $this->display('channel/index');
        }
    }
    //添加分类
    public function add(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['mid']),'绑定模型必须选择'),array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data = postinput($_POST,array('fid'));
            if($data['type'] == 2){
                if(!$data['url']){
                    $this->jsonMsg('栏目URL没有填写');
                }
            }
            //获取根ID开始
            if($data['pid'] == 0){
                $data['rootid'] = 0;
            }else{
                $root = model('channel')->find('id='.$data['pid'],'id,pid');
                if($root['pid'] == 0){
                    $data['rootid'] = $root['id'];
                }else{
                    $data['rootid'] = $root['pid'];
                }
            }
            //插入模型数据
            $data['uptime'] = time();
            $info = model('channel')->addClass($data);
            if($info){
                $this->jsonMsg('栏目添加成功',1,url('channel/index',array('id'=>$data['pid'])));
            }else{
                $this->jsonMsg('栏目添加失败');
            }
        }else{
            $this->pid = intval($_GET['id']);
            if($this->pid){
                $this->list = model('channel')->find('id='.$this->pid,'mid,language');
            }else{
                $this->list = model('module')->select('type = 0 and locks = 0','name,id');  //模型
                $this->language = $this->model->table('cms_language')->field('id,types,name')->select(); 
            }
            $this->info = model('module')->select('type = 2 and locks = 0','name,id');
            $this->plus = model('module')->select('type = 1 and locks = 0','name,id');
            $this->path = model('channel')->flpath($fid,'channel/index'); //路径
            $this->display('channel/add');
        }   
    }
    //编辑分类
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                cpError::show($msg);
            }
            $fid = intval($_POST['fid']);
            $pid = intval($_POST['pid']);
            $mid = intval($_POST['exts_mid']);
            $_POST['uptime'] = time();
            $data = postinput($_POST,array('fid','pid'));
            $info = model('channel')->editClass($fid,$mid,$data); //插入模型数据
            if($info){
                $this->jsonMsg('栏目编辑成功',1,url('channel/index',array('id'=>$pid))); 
            }else{
                $this->jsonMsg('栏目编辑失败');
            }
        }else{
            $this->fid = intval($_GET['id']);
            $this->pid = intval($_GET['pid']);
            $this->info = model('channel')->find('id = '.$this->fid);
            $this->path = model('channel')->flpath($this->fid,'channel/index'); //当前路径
            $this->language = $this->model->table('cms_language')->order('id desc')->select(); 
            if($this->info['exts_mid']>0){
                $info_ext = model('channel')->editClassExt($this->fid,$this->info['exts_mid']);
                $this->module = model('article')->editInput($this->info['exts_mid'],$info_ext); 
            }
            $this->display('channel/edit'); 
        }
    } 
    //栏目排序
    public function uporter(){
        $fid=  intval($_POST['id']);
        $condition['id'] = $fid;
        $data['neworder'] = intval($_POST['sorter']);
        model('channel')->update($condition,$data);
        return TRUE;
    }
    //低权限管理
    public function channel(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $result =  model('channel')->delClass($fid);
            if($result){
                echo 1;
            }else{
                echo '删除失败!请确认是否包含子分类货对应模型中有数据';
            } 
        }else{
            $this->pid = intval($_GET['id']);  //当前ID
            $condition['id'] = $this->pid;
            $listRows = 20;
            $url = url('channel/channel',array('page'=>'{page}'));
            $allcount = model('channel')->count('pid='.$this->pid);
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->info = model('channel')->find($condition,'type');
            $this->list = model('channel')->classList($this->pid,$limit);
            $this->path = model('channel')->flpath($this->pid,'channel/channel');
            $this->display('channel/channel');
        }
    }
    //添加分类
    public function channeladd(){
      if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['mid']),'绑定模型必须选择'),array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data = postinput($_POST,array('fid'));
            if($data['type'] == 2){
                if(!$data['url']){
                    $this->jsonMsg('栏目URL没有填写');
                }
            }
            //获取根ID开始
            if($data['pid'] == 0){
                $data['rootid'] = 0;
            }else{
                $root = model('channel')->find('id='.$data['pid'],'id,pid');
                if($root['pid'] == 0){
                    $data['rootid'] = $root['id'];
                }else{
                    $data['rootid'] = $root['pid'];
                }
            }
            //插入模型数据
            $data['uptime'] = time();
            $info = model('channel')->addClass($data);
            if($info){
                $this->jsonMsg('栏目添加成功',1,url('channel/channel',array('id'=>$data['pid'])));
            }else{
                $this->jsonMsg('栏目添加失败');
            }
        }else{
            $Cookie = new Cookie();
            $this->pid  = intval($_GET['id']);
            $this->siteid = $Cookie->get('siteid');
            if($this->pid){
                $this->list = model('channel')->find('id='.$this->pid,'mid,language');
            }else{
                $this->list = model('module')->select('type = 0 and locks = 0','name,id');  //模型
                $this->language = $this->model->table('cms_language')->field('id,types,name')->select(); 
            }
            $this->info = model('module')->select('type = 2 and locks = 0','name,id');
            $this->plus = model('module')->select('type = 1 and locks = 0','name,id');
            $this->path = model('channel')->flpath($this->pid,'channel/index'); //路径
            $this->display('channel/channeladd');
        }   
    }
    //低权限管理
    public function channeledit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                cpError::show($msg);
            }
            $fid = intval($_POST['fid']);
            $pid = intval($_POST['pid']);
            $mid = intval($_POST['exts_mid']);
            $_POST['uptime'] = time();
            $data = postinput($_POST,array('fid','pid'));
            $info = model('channel')->editClass($fid,$mid,$data); //插入模型数据
            if($info){
                $this->jsonMsg('栏目编辑成功',1,url('channel/channel',array('id'=>$pid))); 
            }else{
                $this->jsonMsg('栏目编辑失败');
            }
        }else{
            $this->fid = intval($_GET['id']);
            $this->pid = intval($_GET['pid']);
            $this->info = model('channel')->find('id = '.$this->fid);
            $this->path = model('channel')->flpath($this->fid,'channel/channel'); //当前路径
            $this->language = $this->model->table('cms_language')->order('id desc')->select();
            $Cookie = new Cookie();
            $this->siteid = $Cookie->get('siteid');
            if($this->info['exts_mid']>0){
                $info_ext = model('channel')->editClassExt($this->fid,$this->info['exts_mid']);
                $this->module = model('article')->editInput($this->info['exts_mid'],$info_ext); 
            }
            $this->display('channel/channeledit'); 
        }
    }
    //网站站点管理
    public function language(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $condition['id'] = $fid;
            $result =  $this->model->table('cms_language')->where($condition)->delete();
            if($result){
                echo 1;
            }else{
                echo '删除失败!';
            } 
        }else{
            $this->list = $this->model->table('cms_language')->order('id desc')->select(); 
            $this->display('channel/language');
        }
    }
    //网站站点管理
    public function languageadd(){
       if($this->isPost()){
            $msg = Check::rule(
                array(check::must($_POST['name']),'站点名称必须选择'),
                array(check::must($_POST['tpl_index']),'首页模版必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data = postinput($_POST);
            $data['uptime'] = time();
            $info = $this->model->table('cms_language')->data($data)->insert(); 
            if($info){
                $this->jsonMsg('站点添加成功',1);
            }else{
                $this->jsonMsg('站点添加失败');
            }
        }else{
            $this->display('channel/languageadd');
        }   
    }
    //网站站点管理
    public function languageedit(){
        if($this->isPost()){
            $fid = intval($_POST['fid']);
            $data = postinput($_POST,array('fid'));
            $data['uptime'] = time();
            $condition['id'] = $fid;
            $info = $this->model->table('cms_language')->where($condition)->data($data)->update();  //插入模型数据
            if($info){
                $this->jsonMsg('站点编辑成功',1); 
            }else{
                $this->jsonMsg('站点编辑失败');
            }
        }else{
            $this->fid = intval($_GET['id']);
            $condition['id'] = $this->fid;
            $this->info = $this->model->table('cms_language')->where($condition)->find();
            $this->display('channel/languageedit');   
        }
    }
}